package com.example.appmanager.user;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.Appconfig.ToastShow;
import com.example.appmanager.MainActivity;
import com.example.appmanager.R;
import com.example.appmanager.entity.UserTableEntity;
import com.example.appmanager.entity.userLogin;
import com.example.appmanager.services.AllresposeService;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/*
 * 用户登录界面
 * */
public class Login extends AppCompatActivity {
    private ImageView login_imageView;
    private EditText userNameTex;//用户登录名字
    private EditText userPwdTex;//用户密码
    private TextView textForgetPwd;//忘记密码
    private TextView textRegister;//用户注册
    private Button button;//登录按钮
    private Button button_other;//第三方登录按钮
    private String uName;//用户名
    private String userPwd;//用户密码
    private ToastShow toastShow = new ToastShow();
    private UserTableEntity userEntity  = new UserTableEntity();
    String msg = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        initUI();//控件初始化  以及获取界面的初始值
        quireListener();//获取页面的监听
    }
    private void initUI() {
        /*绑定页面的控件*/
        login_imageView = findViewById(R.id.login_imageView);
        userNameTex = findViewById(R.id.editTextUname);//用户登录名字
        userPwdTex = findViewById(R.id.editTextPwd);//用户密码
        textForgetPwd = findViewById(R.id.textView3);//忘记密码  textView4
        textRegister = findViewById(R.id.textView4);//新用户注册
        button = findViewById(R.id.login_button);//登录按钮
        button_other = findViewById(R.id.button_other_login);//登录按钮
    }
    /*验证用户登录
     * */
    private boolean cheakLogin(final String uName, String Pwd) {
        if ((uName.equals("")|| uName.isEmpty()) ||(Pwd.equals("")|| Pwd.isEmpty())){
            Toast.makeText(getApplicationContext(),"用户名或者密码不能为空！！",Toast.LENGTH_LONG).show();
        }
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<UserTableEntity> userCall = request.getUserPwd(uName);
        userCall.enqueue(new Callback<UserTableEntity>() {
            @Override
            public void onResponse(Call<UserTableEntity> call, Response<UserTableEntity> response) {
                assert response.body() != null;
                msg = response.body().getUpassword();
                if (msg.isEmpty()){
                    msg ="请求的数据无效！" ;
                }else {
                    msg = response.body().getUpassword();
                    Log.d("Login",msg);
                    if (msg.equals(userPwd)){
                        userEntity.setUpassword(response.body().getUpassword());
                        userEntity.setUname(response.body().getUname());
                        userEntity.setUaccount(response.body().getUaccount());
                        userEntity.setAge(response.body().getAge());
                        Bundle bundle =new Bundle();
                        bundle.putString("uName",uName);
                        bundle.putString("uRName", response.body().getUname());
                        bundle.putString("uPwd", response.body().getUpassword());

                        bundle.putString("image", response.body().getPicture());
                        bundle.putString("userEntity", (JSONObject.toJSON(userEntity)).toString());
                        Intent intent = new Intent();
                        intent.putExtras(bundle);
                        intent.setClass(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(getApplicationContext(),"密码错误!",Toast.LENGTH_LONG).show();
                    }
                }

            }
            @Override
            public void onFailure(Call<UserTableEntity> call, Throwable t) {
                Log.d("Login", "请求错误" + t.getMessage());
                Toast.makeText(getApplicationContext(),"网络错误！",Toast.LENGTH_LONG).show();
            }
        });
        return false;
    }

    private void quireListener(){
        button_other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = toastShow.getToast(getApplicationContext(),"我们正在努力....!",1000);
                toast.show();
                //Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT).show;
            }
        });
        /*登录按钮 用户提交账号
         * */
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取页面的值
                /*获取用户界面的密码和账号*/
                //用户账号
                uName = userNameTex.getText().toString().trim();
                //用户密码
                userPwd = userPwdTex.getText().toString().trim();
                /*第三方登录按钮*/
                Toast.makeText(getApplicationContext(),"mima"+userPwd+" zhanghao"+uName,Toast.LENGTH_LONG);
                cheakLogin(uName, userPwd);
            }
        });
        /*用户忘记密码 找回密码的按钮*/
        textForgetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), UserFindPassword.class);
                startActivity(intent);
            }
        });
        /*新用户注册*/
        textRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), UserRegister.class);
                startActivity(intent);
            }
        });
    }
}
